package com.test.blogsystem.Utils;

import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.UUID;

public class SecurityUtil {
    // 加密
    public static String encrypt(String password) {
        String salt = UUID.randomUUID().toString().replace("-", "");
        String temp = DigestUtils.md5DigestAsHex((salt + password).getBytes(StandardCharsets.UTF_8));
        return salt + temp;
    }

    // 校验
    public static boolean verify(String password, String sqlPassword) {
        if (!StringUtils.hasLength(password)) return false;
        if (sqlPassword == null || sqlPassword.length() != 64) return false;
        String salt = sqlPassword.substring(0, 32);
        String temp = DigestUtils.md5DigestAsHex((salt + password).getBytes(StandardCharsets.UTF_8));
        return (salt + temp).equals(sqlPassword);
    }
}
